package com.muye.dianli.activity;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;

import org.apache.http.entity.mime.content.StringBody;

import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.muye.constant.C;
import com.muye.constant.ConstantPersonalInfo;
import com.muye.constant.U;
import com.muye.dianli.BaseActivity;
import com.muye.dianli.R;
import com.zvezda.algorithm.utils.MD5;
import com.zvezda.http.utils.HttpConst;
import com.zvezda.http.utils.KeyValue;
import com.zvezda.http.utils.RequestHttpListener;
import com.zvezda.http.utils.RequestHttpPostThread;

/**
 * 意见反馈
 * 
 * @author TangChao
 *
 */
public class FeedbackActivity extends BaseActivity implements OnClickListener, TextWatcher, OnKeyListener
{
	/**
	 * 邮箱地址输入框
	 */
	private EditText inputEmailEdit;
	
	/**
	 * 邮箱地址
	 */
	private String emailStr;
	
	/**
	 * 意见输入框
	 */
	private EditText inputSuggestEdit;
	
	/**
	 * 意见
	 */
	private String suggestStr;
	
	/**
	 * 请求数据线程
	 */
	private RequestHttpPostThread requestHttpPostThread;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		getWindow().setFormat(PixelFormat.RGBA_8888);
		setContentView(R.layout.feedback_layout);
		createTitle();
		initWidget();
	}
	
	@Override
	protected void onDestroy()
	{
		super.onDestroy();
	}
	
	/**
	 * 初始化标题栏
	 */
	private void createTitle()
	{
		TextView titleName = (TextView) findViewById(R.id.title_bar_name_text_view);
		titleName.setText("意见反馈");
		titleName.setTextColor(getResources().getColor(R.color.white));
		ImageView leftBtn = (ImageView) findViewById(R.id.title_bar_left_image_view);
		leftBtn.setImageResource(R.drawable.title_back_icon);
		leftBtn.setVisibility(View.VISIBLE);
		leftBtn.setOnClickListener(this);
	}
	
	/**
	 * 初始化控件
	 */
	private void initWidget()
	{
		inputEmailEdit = (EditText) findViewById(R.id.feedback_email_edit_text);
		inputEmailEdit.addTextChangedListener(this);
		inputSuggestEdit = (EditText) findViewById(R.id.feedback_suggest_edit_text);
		inputSuggestEdit.addTextChangedListener(this);
		TextView confirmBtn = (TextView) findViewById(R.id.feedback_confirm_btn);
		confirmBtn.setOnClickListener(this);
	}
	
	/**
	 * 请求提交反馈
	 */
	private void submitSuggest()
	{
		C.openPragressDialog(this, this, R.string.in_request_data);
		ArrayList<KeyValue<String, Object>> paramsList = new ArrayList<KeyValue<String, Object>>();
		String str = C.APPID + suggestStr + emailStr + ConstantPersonalInfo.name + "" + C.CONSTANT;
		try
		{
			paramsList.add(new KeyValue<String, Object>("appId", new StringBody(C.APPID, Charset.forName("utf-8"))));
			paramsList.add(new KeyValue<String, Object>("content", new StringBody(suggestStr, Charset.forName("utf-8"))));
			paramsList.add(new KeyValue<String, Object>("email", new StringBody(emailStr, Charset.forName("utf-8"))));
			paramsList.add(new KeyValue<String, Object>("name", new StringBody(ConstantPersonalInfo.name, Charset
					.forName("utf-8"))));
			paramsList.add(new KeyValue<String, Object>("phone", new StringBody("", Charset.forName("utf-8"))));
			paramsList
					.add(new KeyValue<String, Object>("sign", new StringBody(MD5.getMD5_32(str), Charset.forName("utf-8"))));
		}
		catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
		requestHttpPostThread = new RequestHttpPostThread(submitSuggestListener, U.SUBMIT_SUGGEST, paramsList);
		requestHttpPostThread.start();
	}
	
	/**
	 * 请求提交反馈返回监听
	 */
	private RequestHttpListener submitSuggestListener = new RequestHttpListener()
	{
		
		@Override
		public void requestHttp(Message msg)
		{
			Bundle bundle = msg.getData();
			int result = bundle.getInt(HttpConst.ACTION_RESULT, HttpConst.ACTION_FAIL);
			if (result == HttpConst.ACTION_SUCCESS)
			{
				byte[] dataArr = bundle.getByteArray(HttpConst.DATA_ARR);
				
				try
				{
					String str = new String(dataArr, "utf-8");
					Log.i(C.TAG, "提交反馈结果----->" + str);
				}
				catch (UnsupportedEncodingException e)
				{
					e.printStackTrace();
				}
				Message message = new Message();
				message.what = 0;
				message.arg1 = result;
				handler.sendMessage(message);
			}
		}
	};
	
	/**
	 * 处理数据
	 */
	private Handler handler = new Handler()
	{
		
		@Override
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			C.closePragressDialog();
			switch (msg.what)
			{
				case 0:
					switch (msg.arg1)
					{
						case HttpConst.ACTION_SUCCESS:
							C.showToast(FeedbackActivity.this, "提交成功");
							finish();
							break;
						case HttpConst.ACTION_FAIL:
							C.showToast(FeedbackActivity.this, "提交失败,请重试");
							break;
						
						default:
							break;
					}
					break;
				
				default:
					break;
			}
		}
		
	};
	
	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.title_bar_left_image_view:
				finish();
				break;
			case R.id.feedback_confirm_btn:// 确定按钮
				if (emailStr == null || emailStr.equals(""))
				{
					emailStr = "";
				}
				if (suggestStr == null || suggestStr.equals(""))
				{
					C.showToast(this, "请输入您的意见或建议");
					return;
				}
				submitSuggest();
				break;
			
			default:
				break;
		}
	}
	
	@Override
	public void afterTextChanged(Editable s)
	{
		emailStr = inputEmailEdit.getText().toString();
		suggestStr = inputSuggestEdit.getText().toString();
	}
	
	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after)
	{
		
	}
	
	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count)
	{
		
	}
	
	@Override
	public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
	{
		return false;
	}
}
